package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private EmpService empService;
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult pageByCondition(String name, LocalDate begin, LocalDate end, int page, int pageSize) {
        Page<Clazz> clazzPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Clazz> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null && name.length() > 0, Clazz::getName, name);
        lqw.between(begin != null && end != null, Clazz::getEndDate, begin, end);

        clazzPage = this.page(clazzPage, lqw);
        List<Clazz> records = clazzPage.getRecords();

        //结课时间>now,在读
        //开课时间>now,为开班
        records.forEach(clazz -> {
            if (clazz.getEndDate().isAfter(LocalDate.now())) {
                clazz.setStatus("在读");
            }
            if (clazz.getBeginDate().isAfter(LocalDate.now())) {
                clazz.setStatus("未开班");
            }
            if (clazz.getEndDate().isBefore(LocalDate.now())) {
                clazz.setStatus("已结课");
            }
        });

        //可能会有null,所以将null给过滤掉
        Set<Integer> masterIds = records.stream().map(clazz -> clazz.getMasterId()).filter(Objects::nonNull).collect(Collectors.toSet());
        if (masterIds != null && masterIds.size() > 0) {
            List<Emp> emps = empService.listByIds(masterIds);
            Map<Integer, String> collect = emps.stream().collect(Collectors.toMap(Emp::getId, Emp::getName));
            for (Clazz record : records) {
                record.setMasterName(collect.get(record.getMasterId()));
            }
        }
        return new PageResult(clazzPage.getTotal(), records);
    }

    @Override
    public void addStudent(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.insert(clazz);
    }

    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateById(clazz);
    }

    @Override
    public void removeClazzById(Integer id) {
        //先查询改班级下是否有学生，如果有，就不能删除
        List<Student> list = studentMapper.selectList(new LambdaQueryWrapper<Student>().eq(Student::getClazzId, id));
        if (list != null && list.size() > 0) {
            throw new RuntimeException("班级还有学生不能删除!");
        }
        //没有学会生，删除班级
        this.removeById(id);
    }
}
